﻿@using Resource;
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    @{ await Html.RenderPartialAsync("/Views/Shared/_LayoutScript.cshtml"); }
    @{ await Html.RenderPartialAsync("/Views/Shared/_StyleCss.cshtml"); }
    @*<!-- 引入 xlsx 库<script src="https://cdn.bootcdn.net/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>-->*@
    <script src="/lib/xlsx/xlsx.full.min.js?v=@DateTime.Now.Ticks"></script>

    <link href="/css/TesterProd.css?v=@DateTime.Now.Ticks" rel="stylesheet" />
    <title>TesterProdNew</title>
    <style type="text/css">
        /*报表样式*/
        html, body {
            background: url(../images/ProcessCapacity/bgwhite.png) no-repeat;
            background-size: cover;
            font-size: .16rem;
            color: #ffffff;
            position: relative;
            width: 100%;
            height: 100%;
            font-family: myFirstFont !important;
            color: black;
        }

        .rightTop {
            background-color: #f7fdff;
        }

        .dropbtn, button {
            background-color: #0aa9cd;
        }

        #query > button {
            background-color: #0aa9cd;
            border: 2px solid #0aa9cd;
        }

        .dropdown-content {
            background-color: #12bed0;
        }

        .Zebra {
            background-color: #e5ecff;
        }

        .ZebraNo {
            background-color: #e8f1f9;
        }

        .ZebraTo {
            background-color: #ffffff;
        }

        .warn {
            background-color: #ffcccc;
        }

        .note {
            color: red;
            font-size: 0.15rem;
            font-weight: bolder;
        }

        .table-title {
            color: #19adcf;
            font-size: 0.15rem;
            font-weight: bolder;
        }

        .jobnum:hover {
            background-color: #b3f1fe;
        }

        /*END*/

        .truncate {
            max-width: 1.2rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        /*明细模态*/
        .show_mode {
            border-radius: 0.1rem;
            position: absolute;
            z-index: 99;
            height: 10.6rem;
            width: 100%;
            /*text-align: center;*/
            background: #a2c2e2;
            display: none;
        }

        .contentdetail {
            background-color: white;
            height: 10.4rem;
            width: 100%;
            padding: .1rem .3rem .1rem .3rem;
            color: black;
        }

        .show_mode > .contentdetail > div {
            color: black;
            height: 33.3%;
            width: 50%;
            float: left;
            border-block: solid #ffffff 1px;
        }

        .show_mode > .contentdetail > div > label {
            color: black;
        }

        .detail {
            color: black;
            background-color: #dfe4ee;
            height: 3rem;
            width: 100%;
        }

        .mode_close {
            float: right;
        }

        .mode_close:hover {
            background-color: #848690;
        }
        /*end*/
        #btn_export {
            height: 0.3rem;
            font-size: 0.16rem;
            margin-left: 2px;
            border-radius: 2px;
            background-color: #e9e9ed;
            border: 1px solid #8f8f9d;
        }

        #btn_export:hover {
            background-color: #dddddd;
        }

        #left-table, #tester_end, #process_output {
            background-image: url('../images/loading.gif');
            background-size: 0.8rem 0.8rem;
            background-repeat: no-repeat;
            background-position: center;
        }

    </style>
</head>
<body class="fill-parent" style="overflow-y: hidden; color: black;">
    <div class="main">
        <div class="header">
            <div class="header-left fl">
                <img src="/images/logo.png?v=@DateTime.Now.Ticks" class="scaled-image" draggable="false" oncontextmenu="return false;" ondragstart="return false;" />
            </div>
            <div class="btn">
                <spn>
                    <div id="query">
                        <div class="dropdown">
                            <button class="dropbtn">@Label.Desktop</button>
                            <div class="dropdown-content">
                                <a class="menudropdown" href=@Html.Raw($"https://dashboard.li.lumentuminc.net/API/DesktopApp/Dashboard.exe?v={DateTime.Now.Ticks}") download>@Label.Local</a>
                                <a class="menudropdown" href=@Html.Raw($"https://dashboard.li.lumentuminc.net/API/DesktopApp/Dashboard_lnk.zip?v={DateTime.Now.Ticks}") download>@Label.Shortcut</a>
                            </div>
                        </div>
                        <div class="dropdown">
                            <button class="dropbtn">@Label.Data@Label.Datum</button>
                            <div class="dropdown-content">
                                <a class="menudropdown" onclick="TesterProdExport()">@Label.Report@Label.Export</a>
                            </div>
                        </div>
                        <div class="dropdown">
                            <button class="dropbtn">@Label.Language</button>
                            <div class="dropdown-content">
                                <a class="menudropdown" onclick="setLanguageCookie('@Const.CNLANGUAGE')">@Label.Chinese</a>
                                <a class="menudropdown" onclick="setLanguageCookie('@Const.USLANGUAGE')">@Label.English</a>
                            </div>
                        </div>
                        <button class="modalBtn dimension">@Label.Full</button>
                    </div>
                </spn>
            </div>
            <div class="header-center fl">
                <div class="header-title"></div>
                <div class="header-img"></div>
            </div>
            <div class="header-right fl" id="time"></div>
            <div class="header-bottom fl"></div>
        </div>
        <div class="center">
            <div class="center-left fl">
                <div class="left-top rightTop border">
                    <div class="title note">@Label.Note_Equipment_Output_Report</div>
                    <div id="board" class="chart">
                        <div id="table-container">
                            <div id="left-table" class="rightTop">
                                <p class="title table-title">@Html.Raw(Label.TesterYield)</p>
                                <div id="left-table-body"></div>
                            </div>
                            <div id="right-table">
                                <div id="tester_end" class="rightTop">
                                    <p class="title note">@Html.Raw(Label.TesterDown)</p>
                                    <div id="tester_end_table_body"></div>
                                </div>
                                <div id="process_output" class="rightTop">
                                    <p class="title table-title">@Html.Raw(Label.TesterDetail)</p>
                                    <div id="process_output_table_body"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="show_mode">
            <div style="width:100%; height:0.3rem;">
                <div class="mode_close" style="height:100%;margin-right:10px;line-height:0.3rem;"><label style="font-size:0.17rem;color:white;">关闭&times;</label></div>
            </div>
            <div class="contentdetail"></div>
        </div>
    </div>

    <script type="text/javascript" href="?v=@DateTime.Now.Ticks">
        $(".show_mode").css("display", "none");
        $('.mode_close').click(function () {
            $(".show_mode").css("display", "none");
            $(".contentdetail").empty();
            $(".contentdetail div").remove();
        });
        $(document).ready(function () {
            var whei = $(window).height();
            $("html").css({ fontSize: whei / 12.5 });
            $(window).resize(function () {
                if ($(".dimension").text() == "@Html.Raw(Label.Full)") {
                    var whei = $(window).height();
                    $("html").css({ fontSize: whei / 12.5 });
                }
                else {
                    var whei = $(window).width();
                    $("html").css({ fontSize: whei / 22 })
                }
            });

            function numberToUpperCase(number) {
                var uppercaseMonths = [
                    '一月', '二月', '三月', '四月', '五月', '六月',
                    '七月', '八月', '九月', '十月', '十一月', '十二月'
                ];
                return uppercaseMonths[number - 1];
            }
            var myDate = new Date();
            var result = numberToUpperCase(myDate.getMonth() + 1);
            $('.header-title').html("@Html.Raw(Label.Board_Equipment_Output_Report)");
            function getTime() {
                var myDate = new Date();
                var myYear = myDate.getFullYear();
                var myMonth = myDate.getMonth() + 1;
                var myToday = myDate.getDate();
                var myDay = myDate.getDay();
                var myHour = myDate.getHours();
                var myMinute = myDate.getMinutes();
                var mySecond = myDate.getSeconds();
                var week = ['@Label.Sunday', '@Label.Monday', '@Label.Tuesday', '@Label.Wednesday', '@Label.Thursday', '@Label.Friday', '@Label.Saturday'];
                var nowTime = myYear + '-' + fillZero(myMonth) + '-' + fillZero(myToday) + '&nbsp;&nbsp;' + fillZero(myHour) + ':' + fillZero(myMinute) + ':' + fillZero(mySecond) + '&nbsp;&nbsp;' + week[myDay] + '&nbsp;&nbsp;';
                $('#time').html(nowTime);
            };
            function fillZero(str) {
                var realNum;
                if (str < 10) {
                    realNum = '0' + str;
                } else {
                    realNum = str;
                }
                return realNum;
            }
            setInterval(getTime, 1000);
            $('.dimension').on('click', function (e) {
                var element = document.documentElement;
                if ($(".dimension").text() == "@Html.Raw(Label.Full)") {
                    if (element.requestFullscreen) {
                        element.requestFullscreen();
                    } else if (element.mozRequestFullScreen) {
                        element.mozRequestFullScreen();
                    } else if (element.webkitRequestFullscreen) {
                        element.webkitRequestFullscreen();
                    } else if (element.msRequestFullscreen) {
                        element.msRequestFullscreen();
                    }
                    $(".dimension").text("@Html.Raw(Label.Exit+Label.Full)");
                } else {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                    $(".dimension").text("@Html.Raw(Label.Full)");
                }
            });
        });

        //json toXlsx
        function exportXlsx(data,name) {
            const book = XLSX.utils.book_new();
            const sheet = XLSX.utils.json_to_sheet(data);
            XLSX.utils.book_append_sheet(book, sheet);
            XLSX.writeFile(book, `${name}.xlsx`);
        }
        //exportXlsx(test_data,'Export');

        //定时查询
        var Tabtimer;
        function TabInterval() {
            clearInterval(Tabtimer);
            //每隔10m执行一次
            Tabtimer = setInterval(function () {
                getdate();
            }, 10 * 60 * 1000);
        }

        $(function () {
            setTimeout(function () {
                Getdata();
            }, 500);
        })

        var Getdata = function () {
            getdate();
            TabInterval();
        }

        //Table联动
        var tester_select = null;
        var tester_data_up = function (tester) {
            tester_select = tester;
            tester_end_boarddata();
            tester_process_boarddata();
        }

        function loadingshow() {
            $('#left-table,#tester_end,#process_output').css('background-image', 'url("../images/loading.gif")');
            $('#left-table,#tester_end,#process_output').css('background-size', '0.8rem 0.8rem');
            $('#left-table,#tester_end,#process_output').css('background-repeat', 'no-repeat');
            $('#left-table,#tester_end,#process_output').css('background-position', 'center');
        }
        function loadingnone() {
            $('#left-table,#tester_end,#process_output').css('background-image', 'none');
        }

        //获取数据
        var Tab;
        var getdate = function () {
            loadingshow();
            $("#left-table-body,#tester_end_table_body,#process_output_table_body").empty();
            $.ajax({
                type: "POST",
                url: api + "/ReportAction/GetTesterProd",
                dataType: "json",
                async: true,
                success: function (dt) {
                    loadingnone();
                    tester_select = null;
                    Tab = dt.ReturnData;
                    tester_prod_boarddata();
                    tester_end_boarddata();
                    tester_process_boarddata();
                    Tab.Table3.forEach(item => {
                        item.START_TIME = item.START_TIME.replace("T", " ");
                        item.TEST_TIME = item.TEST_TIME.replace("T", " ");
                    });
                }
            });
        }

        //报表导出
        var TesterProdExport = function () {
            exportXlsx(Tab.Table3, "Test_DATA");
            /*
            var formData = new FormData();
            formData.append('proj_code', $('#cmb_proj_code').val());
            formData.append('product_code', $("#cmb_product_code").val());
            ExportType = type;
            var url = api + "/ReportAction/ErpWipReportToExcel";
            PostExportToExcel(url, formData);
            */
        }
        /*导出生产数据*/
        var TestDataExport = function (type) {
            ExportType = type;
            var formData = new FormData();
            formData.append('type', ExportType);
            formData.append('proj_code', $('#cmb_proj_code').val());
            formData.append('process_id', $('#cmb_process_code').val() == null ? "ALL" : $('#cmb_process_code').val());
            formData.append('start_time', $("#startdate").val());
            formData.append('end_time', $("#enddate").val());
            var url = api + "/Dashboard/TestDataToExcel";
            PostExportToExcel(url, formData);
        }
        //POST 参数不固定 导出
        var PostExportToExcel = function (url, formData) {
            //GET: var url = "http://localhost:44304/Dashboard/ExportToExcel?" + $.param(pam);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = 'blob';
            xhr.onload = function () {
                if (xhr.status === 200) {
                    var url = window.URL.createObjectURL(xhr.response);
                    var a = document.createElement('a');
                    a.href = url;
                    a.download = ExportType.toLocaleUpperCase() + '.xlsx';
                    a.click();
                    window.URL.revokeObjectURL(url);
                }
            };
            xhr.send(formData);
        }
        //导出
        var ExportToExcel = function (url) {
            //var url = "http://localhost:44304/Dashboard/ExportToExcel?" + $.param(pam);
            var xhr = new XMLHttpRequest();
            xhr.open('GET', url, true);
            xhr.responseType = 'blob';
            xhr.onload = function () {
                if (xhr.status === 200) {
                    var url = window.URL.createObjectURL(xhr.response);
                    var a = document.createElement('a');
                    a.href = url;
                    a.download = ExportType.toLocaleUpperCase() + '.xlsx';
                    a.click();
                    window.URL.revokeObjectURL(url);
                }
            };
            /*
            GET:param
            var formData = new FormData();
            formData.append('CL_TYPE', $("#cmb_classtype").val());
            xhr.send(formData);
            */
            xhr.send();
        }
        //合并相同行
        function mergeFunc(tableId) {
            var tab = document.getElementById(tableId);
            var maxCol = 3;//maxcol用于设置需要合并的列数
            var count, start;
            for (var col = maxCol - 1; col >= 0; col--) {
                //用于存储相同个数
                count = 1;
                for (var i = 0; i < tab.rows.length; i++) {
                    if (i > 0 && col > 0 && tab.rows[i].cells[col].innerHTML == tab.rows[i - 1].cells[col].innerHTML
                        && tab.rows[i].cells[col - 1].innerHTML == tab.rows[i - 1].cells[col - 1].innerHTML) {
                        count++;
                    } else if (i > 0 && col == 0 && tab.rows[i].cells[col].innerHTML == tab.rows[i - 1].cells[col].innerHTML) {
                        count++;
                    } else {
                        if (count > 1) {
                            start = i - count;
                            tab.rows[start].cells[col].rowSpan = count;
                            for (var j = start + 1; j < i; j++) {
                                tab.rows[j].removeChild(tab.rows[j].cells[col]);
                            }
                            count = 1;
                        }
                    }
                }
                if (count > 1) {
                    start = i - count;
                    tab.rows[start].cells[col].rowSpan = count;
                    for (var j = start + 1; j < i; j++) {
                        tab.rows[j].removeChild(tab.rows[j].cells[col]);
                    }
                }
            }
        }

        //当天设备产出
        var tester_prod_boarddata = function () {
            var data = Tab.Table;
            $("#left-table-body").empty();
            var left_table_html = `
             <table id="myTable" class="DataSheet" border="1" style="width: 100%; height: 100%;">
                <thead><tr></tr></thead>
                <tbody id="yield_tbody"></tbody>
            </table>`;
            $("#left-table-body").append(left_table_html);
            var table = $('#myTable');
            var tbody = table.find('tbody');
            var headerRow = table.find('thead tr');
            var headers = Object.keys(data[0]);
            headers.forEach(function (header) {
                headerRow.append('<th>' + header + '</th>');
            });
            data.forEach(function (row, index) {
                index++;
                var newRow = $(`<tr class="${(index % 2) > 0 ? "ZebraNo" : "ZebraTo"}"></tr>`);
                headers.forEach(function (header, columnindex) {
                    if (typeof row[header] === "number") {
                        if (header === "YIELD") {
                            row[header] = parseFloat(row[header]).toFixed(2) + '%';
                        } else {
                            row[header] = row[header].toFixed(0);
                        }
                    }
                    if (header === "TESTER") {
                        newRow.append(`<td class="truncate jobnum" onclick="tester_data_up('${row[header]}');"><a style="${row["DOWN"] == "Y" ? "color:red" : "color:blue"};font-weight:bolder;">${row[header]}</a></td>`);
                    } else {
                        if ((header === "YIELD") && parseInt(row[header].replace(/\%/g, '')) <= 90) {
                            newRow.append(`<td class="truncate warn">${row[header]}</td>`);
                        } else {
                            newRow.append(`<td class="truncate">${row[header]}</td>`);
                        }
                    }
                });
                tbody.append(newRow);
            });
        }
        //最近一小时未产出设备
        var tester_end_boarddata = function () {
            var data = Tab.Table1;
            $("#tester_end_table_body").empty();
            var tester_end_html = `
            <table id="tester_end_table" class="DataSheet" border="1" style="background-color:#dfe4ee; width: 100%; height: 100%;">
                <thead><tr></tr></thead>
                <tbody id="tester_end_tbody"></tbody>
            </table>`;
            $("#tester_end_table_body").append(tester_end_html);
            var table = $('#tester_end_table');
            var tbody = table.find('tbody');
            var headerRow = table.find('thead tr');
            var headers = Object.keys(data[0]);
            headers.forEach(function (header) {
                headerRow.append('<th>' + header + '</th>');
            });
            data.forEach(function (row, index) {
                index++;
                var newRow = $(`<tr class="${(index % 2) > 0 ? "ZebraNo" : "ZebraTo"}"></tr>`);
                headers.forEach(function (header, columnindex) {
                    if (typeof row[header] === "number") {
                        if (header === "YIELD") {
                            row[header] = parseFloat(row[header]).toFixed(2) + '%';
                        } else {
                            row[header] = row[header].toFixed(0);
                        }
                    }
                    if (header === "END_TIME") {
                        row[header] = row[header].replace(/\T/g, '&emsp;');
                    }
                    if (header === "TESTER") {
                        newRow.append(`<td class="truncate jobnum" onclick="tester_data_up('${row[header]}');"><a style="color:red;font-weight:bolder;">${row[header]}</a></td>`);
                    } else {
                        if ((header === "YIELD") && parseInt(row[header].replace(/\%/g, '')) <= 90) {
                            newRow.append(`<td class="truncate warn">${row[header]}</td>`);
                        } else {
                            newRow.append(`<td class="truncate">${row[header]}</td>`);
                        }
                    }

                });
                tbody.append(newRow);
            });
        }
        //当天设备产出明细
        var tester_process_boarddata = function () {
            var data;
            if (tester_select == null) {
                tester_select = Tab.Table[0].TESTER;
            }
            var testerData = Tab.Table2.filter(function (item) {
                return item.TESTER === tester_select;
            });
            data = testerData;
            $("#process_output_table_body").empty();
            var process_output_html = `
            <table id="process_output_table" class="DataSheet" border="1" style="background-color:#dfe4ee; width: 100%; height: 100%;">
                <thead><tr></tr></thead>
                <tbody id="process_output_tbody"></tbody>
            </table>`;
            $("#process_output_table_body").append(process_output_html);
            var table = $('#process_output_table');
            var tbody = table.find('tbody');
            var headerRow = table.find('thead tr');
            var headers = Object.keys(data[0]);
            headers.forEach(function (header) {
                headerRow.append('<th>' + header + '</th>');
            });
            data.forEach(function (row, index) {
                index++;
                var newRow = $(`<tr class="${(index % 2) > 0 ? "ZebraNo" : "ZebraTo"}"></tr>`);
                headers.forEach(function (header, columnindex) {
                    if (typeof row[header] === "number") {
                        if (header === "YIELD") {
                            row[header] = parseFloat(row[header]).toFixed(2) + '%';
                        } else {
                            row[header] = row[header].toFixed(0);
                        }
                    }
                    if (header.includes("TIME")) {
                        row[header] = row[header].replace(/\T/g, '&emsp;');
                    }
                    if ((header === "YIELD") && parseInt(row[header].replace(/\%/g, '')) <= 90) {
                        newRow.append(`<td class="truncate warn">${row[header]}</td>`);
                    } else {
                        newRow.append(`<td class="truncate ${parseInt(row["END_MINUTE"]) >= 60 ? "warn" : null}">${row[header]}</td>`);
                    }
                });
                tbody.append(newRow);
            });
        }
        //END
    </script>
</body>
</html>
